/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.calimeraw.testform.controller;

import com.calimeraw.testform.NameService;
import com.calimeraw.testform.model.Name;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

/**
 *
 * @author Calimeraw93
 */
@Controller
public class FormNameController {
    
    @Autowired
    private NameService nameService;

    public void setNameService(NameService nameService) {
        this.nameService = nameService;
    }
    
    @RequestMapping(value="/helloForm.htm", method=RequestMethod.GET)
    public ModelAndView initForm()
    {
        ModelAndView mv = new ModelAndView("helloForm");   
        mv.addObject("name", new Name()); // Pour le formulaire représente le model attribute.
                                          // Commente le pour voir ce qu'il se passe si il n'y est pas.
        return mv;
    }
    
    @RequestMapping(value="/helloForm.htm", method=RequestMethod.POST)
    public ModelAndView submitForm(@ModelAttribute("name") Name name,
                                   BindingResult result,
                                   SessionStatus status)
    {
        nameService.setName(name.getName());
        status.setComplete();
        return new ModelAndView(new RedirectView("./helloView.htm"),
                                "name",
                                nameService.getName());
    }
}
